home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 2 / CD ACTUAL VOL 2.iso / docs / mini / key-setu < prev    next >
Encoding:
Text File  |  1995-09-10  |  17.7 KB  |  582 lines

  1.   Linux    Keyboard Setup Mini-Howto
  2.   Stephen Lee, sl14@cornell.edu
  3.   Version 1.2, 13 May 1995
  4.  
  5.   This Mini-HOWTO document describes setting up    the kernel and applica-
  6.   tions    to handle the Cursor control keys.
  7.  
  8.   1.  Introduction
  9.  
  10.  
  11.   It has been annoying to me that cursor keys had not work consistently
  12.   across different programs (and on different machines), so I took some
  13.   time and tried to fix    all that.  Here    I document my experience so
  14.   others would not need    to go through the same tedious cycle I did.
  15.  
  16.   I'm using Slackware 2.0.1, so    pathnames to files might be different
  17.   from yours if    you are    using a    different distribution.
  18.  
  19.   Some of the material here appeared in    an earlier ``BackSpace Mini-
  20.   HOWTO''.  Although the method    described there    still works, I consider
  21.   this a better    solution.
  22.  
  23.  
  24.   1.1.    Typography
  25.  
  26.  
  27.  
  28.   o  This is a program name.
  29.  
  30.   o  This is a ``command'' you'd type on a keyboard.
  31.  
  32.   o  This is a <Key> on    the keyboard.  eg. <BackSpace>,    <Delete>,
  33.      <Shift-l>,    <Ctrl-q> etc.
  34.  
  35.   o  This is an    [X11 Keysym] which you can use for mapping keys    under X.
  36.      eg. [BackSpace], [Delete],    [Left],    [Home] etc.
  37.  
  38.  
  39.   1.2.    Terminalogy
  40.  
  41.  
  42.  
  43.      ESC
  44.     ASCII character    0x1B.
  45.  
  46.  
  47.      BS    ASCII character    0x08, or control-h.
  48.  
  49.  
  50.      DEL
  51.     ASCII character    0x7F.
  52.  
  53.  
  54.      ^D    ASCII character    0x04, or control-d.
  55.  
  56.  
  57.      VC    A Linux    Virtual    Console.
  58.  
  59.  
  60.  
  61.  
  62.   1.3.    Acknowledgements
  63.  
  64.  
  65.   Thanks to the    following people who commented on my ``BackSpace Mini-
  66.   HOWTO'':
  67.   John Copella,    Andrew Rakowski, Dr. Jacques Gelinas, Michael Bischoff,
  68.   Topher Hughes, Chuck Meyer, Alexis Kotte, and    especially Ted Stern and
  69.   Steve    Dunham.
  70.  
  71.  
  72.   2.  Non-X configuration
  73.  
  74.  
  75.   I assume you came from the DOS world like I did, and is used to the
  76.   mapping of function that <BackSpace> deletes character to the    left of
  77.   the cursor and <Delete> deletes character on top of the cursor.
  78.  
  79.   Under    a shell, the most intuitive mapping is <BackSpace> -> BS and
  80.   <Delete> -> DEL.  This is fine unless    you want to use    EMACS. EMACS map
  81.   <Ctrl-h> to its help function, which,    under ASCII, is    unfortunately
  82.   BS.  So each time you    want to    erase a    character backwards, you invoke
  83.   the help system.  Also, DEL under Emacs deletes BACKWARDS, like what
  84.   you'd    expect for BS.
  85.  
  86.   One choice is    to remap the keys under    Emacs.    Unfortunately you'll
  87.   lose the ``<Ctrl-h> =    help'' mapping.     So, I decided to map
  88.   <BackSpace> -> DEL.  This leaves BS for use by <Ctrl-h> in Emacs.
  89.  
  90.   So, now what shall we    do for the <Delete> key?  In a previous    verion
  91.   of this document I used ^D, which works under    both Emacs and csh/tcsh
  92.   as a ``Delete    character on cursor'' function.     But since then    I've
  93.   found    a better solution.  The    kernel by default maps <Delete>    as the
  94.   VT100    ``Remove'' key sequence    (``ESC[3~'').  It is not hard to teach
  95.   Emacs    and tcsh to recognize it.  The advantage is that you can bind it
  96.   differently than <Ctrl-d> in programs.  Also,    it is more consistent if
  97.   you also map other cursor control keys.  The disadvantage is that you
  98.   might    not be able to use it in some application which    you can't bind
  99.   key sequences    (but than apply    to the <Delete>    -> ^D binding as well).
  100.  
  101.  
  102.   2.1.    Linux console
  103.  
  104.  
  105.   Linux    console    key bindings are controlled by the kernel.  The    kernel
  106.   by default generates the correct bindings for    <Backspace> and
  107.   <Delete>, so you should not need to change that.
  108.  
  109.   However, if you do, the following programs (in the 'kbd' package,
  110.   which    should come with Slackware already) affect the key bindings:
  111.  
  112.  
  113.  
  114.      showkeys
  115.     ``showkeys'' shows the Linux keycode generated by a key. The
  116.     keycode    can then be used by loadkeys(1)    to change the keymap.
  117.  
  118.  
  119.      dumpkeys
  120.     Shows the current keybindings. See the manual page for more
  121.     detail.
  122.  
  123.  
  124.      loadkeys
  125.     ``loadkeys file'' loads    keybindings from file ``file''.    Note
  126.     that this changes the key bindings for ALL virtual consoles.
  127.     This is    usually    done at    boot time in /etc/rc.d/rc.local.
  128.  
  129.     You can    start with one of the keytable files in
  130.     /usr/lib/kbd/keytables/*.map and edit that. The    one that is
  131.     compiled into your kernel would    be
  132.     /usr/src/linux/drivers/char/defkeymap.map if you have the kernel
  133.     source.    The format of the file is described in the keytables(5)
  134.     manual page.
  135.  
  136.     The keys of particular interest    are <BackSpace>    (keycode 14),
  137.     <Delete> (111).
  138.  
  139.  
  140.      setmetamode
  141.     ``setmetamode''    controls whether the keystroke <Alt-x>,    where x
  142.     is some    key, would send    the keycode M-x    or the key sequence ESC
  143.     followed by x. This is virtual-console-specific, you can have
  144.     different setting in different virtual consoles.
  145.  
  146.  
  147.  
  148.   2.2.    tty (including the Linux console)
  149.  
  150.  
  151.   ``stty erase <ch>'' tells the    terminal what character    your <BackSpace>
  152.   key generates.  It does NOT change your key bindings.     If you    map
  153.   <BackSpace> to DEL then do a ``stty erase '^H''' it won't magically
  154.   changes your <BackSpace> key to generate ^H; it would    only confuse
  155.   your computer.
  156.  
  157.   To correctly set your    terminal ``erase'' character to    DEL, type ``stty
  158.   erase    '^?''' (where ^? can be    '^' followed by    '?' or <Ctrl-v>
  159.   <BackSpace>) at your shell prompt.  You might    want to    put this in your
  160.   $(HOME)/.cshrc or $(HOME)/.profile.
  161.  
  162.   Note:    although csh/tcsh treats BS and    DEL the    same way, other    programs
  163.   don't.  So your mapping might    seem to    work under csh/tcsh but    you get
  164.   wierd    stuff like ^? under some programs.  Solution: remember the
  165.   ``stty'' command above.
  166.  
  167.  
  168.   2.3.    Shells
  169.  
  170.  
  171.  
  172.   2.3.1.  tcsh
  173.  
  174.  
  175.   tcsh provides    the command ``bindkey''    for binding keys:
  176.  
  177.  
  178.      bindkey
  179.     lists all current bindings.
  180.  
  181.  
  182.      bindkey ``str'' function
  183.     binds input string ``str'' to ``function''.  A list of tcsh
  184.     functions can be obtained by ``bindkey -l''.
  185.  
  186.  
  187.      bindkey -k    <up | down | left | right> function
  188.     binds an arrow key to ``function''.
  189.  
  190.  
  191.   Example: To bind cursor control keys,    put this in $HOME/.cshrc:
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.   ______________________________________________________________________
  200.       if ($term    == "xterm" || $term == "vt100" \
  201.         || $term ==    "vt102"    || $term !~ "con*") then
  202.       # bind keypad    keys for console, vt100, vt102,    xterm
  203.       bindkey "\e[1~" beginning-of-line  # Home
  204.       bindkey "\e[2~" overwrite-mode     # Ins
  205.       bindkey "\e[3~" delete-char         # Delete
  206.       bindkey "\e[4~" end-of-line         # End
  207.       endif
  208.   ______________________________________________________________________
  209.  
  210.  
  211.  
  212.   See the man page for tcsh(1) for a complete description.
  213.  
  214.   Example: You can have    4DOS-style command history under tcsh with the
  215.   tcsh function-pair history-search-backward (Meta-p) and history-
  216.   search-forward (Meta-n).  Typing ``abc<Meta-p>'' will    only show lines
  217.   in history that start    with ``abc''.  Also, function magic-space
  218.   expands !  histories as you type, and    I prefer them over the default
  219.   so I bound them to the up/down arrow keys and    space:
  220.  
  221.  
  222.   ______________________________________________________________________
  223.       bindkey -k up history-search-backward
  224.       bindkey -k down history-search-forward
  225.       bindkey "    " magic-space
  226.   ______________________________________________________________________
  227.  
  228.  
  229.  
  230.  
  231.   2.3.2.  bash
  232.  
  233.  
  234.   The ``.inputrc'' file    contains the list of key bindings to functions.
  235.  
  236.   For example, if you put the following    line in    $HOME/.inputrc:
  237.  
  238.  
  239.   ______________________________________________________________________
  240.       "\e[1~": beginning-of-line
  241.       "\e[3~": delete-char
  242.       "\e[4~": end-of-line
  243.   ______________________________________________________________________
  244.  
  245.  
  246.  
  247.   It will map the <Home>, <Delete>, and    <End> keys respectively    to the
  248.   corresponding    functions.
  249.  
  250.   You might also need the following line if your <BackSpace> key sends
  251.   the ASCII DEL:
  252.  
  253.  
  254.   ______________________________________________________________________
  255.       DEL: backward-delete-char
  256.   ______________________________________________________________________
  257.  
  258.  
  259.  
  260.   You probably don't need it (I    don't),    but it's worth a try if    you run
  261.   into trouble.
  262.  
  263.  
  264.  
  265.   2.4.    Editors
  266.  
  267.  
  268.  
  269.   2.4.1.  Emacs    19
  270.  
  271.  
  272.   Emacs    19 provides the    elisp function define-key for binding keys.  You
  273.   can bind a key sequence to a function    key like this in $(HOME)/.emacs:
  274.  
  275.  
  276.   ______________________________________________________________________
  277.       ;; map function keys on PC keyboard
  278.       (setq term (getenv "TERM"))
  279.       (if (or
  280.        (string= "xterm" term)
  281.        (string= "con" (substring term 0 3))    ; linux    consoles
  282.        (string= "vt100" term)
  283.        (string= "vt102" term))
  284.       (progn
  285.         (defun my-setkey-hook ()
  286.           (define-key function-key-map "\e[1~" [home])
  287.           (define-key function-key-map "\e[2~" [insert])
  288.           (define-key function-key-map "\e[3~" [delete])
  289.           (define-key function-key-map "\e[4~" [end])
  290.           ;; these are just    my own sequences
  291.           ;; so I can use the keys under Emacs
  292.           (define-key function-key-map "\e[40~" [C-prior])
  293.           (define-key function-key-map "\e[41~" [C-next])
  294.           ;; function keys:    use same mapping as xterm
  295.           (define-key function-key-map "\e[11~" [f1])
  296.           (define-key function-key-map "\e[12~" [f2])
  297.       ;; ...
  298.           (define-key function-key-map "\e[24~" [f12])
  299.           (define-key function-key-map "\e[25~" [S-f1])
  300.           (define-key function-key-map "\e[26~" [S-f2])
  301.       ;; ...
  302.           (define-key function-key-map "\e[39~" [S-f12])
  303.           )
  304.         (add-hook 'term-setup-hook 'my-setkey-hook)
  305.         )
  306.     ()
  307.     )
  308.   ______________________________________________________________________
  309.  
  310.  
  311.  
  312.   Note the function key    names are all in lowercase.
  313.  
  314.   You can also bind function keys (or key sequences, but we won't be
  315.   using    that here) to a    emacs command with global-set-key like this:
  316.  
  317.  
  318.   ______________________________________________________________________
  319.       (global-set-key [delete] 'delete-char)
  320.       (global-set-key [home] 'beginning-of-line)      ;    you might not want this
  321.       (global-set-key [end] 'end-of-line)          ;    nor this
  322.       (global-set-key [C-prior]    'beginning-of-buffer)
  323.       (global-set-key [C-next] 'end-of-buffer)
  324.  
  325.       (global-set-key [f1] 'help-for-help)
  326.       ;; ...
  327.       (global-set-key [S-f12] 'info)
  328.   ______________________________________________________________________
  329.  
  330.  
  331.   2.4.2.  vi
  332.  
  333.  
  334.   Anybody want to write    this?
  335.  
  336.  
  337.   2.5.    Other Programs
  338.  
  339.  
  340.  
  341.   2.5.1.  less
  342.  
  343.  
  344.   lesskey(1) allows you    to map keys for    the less(1) pager.  Put    the
  345.   following lines in your $HOME/.lessrc    (this is for VT100-like
  346.   terminals):
  347.  
  348.  
  349.   ______________________________________________________________________
  350.       ^[[A   back-line
  351.       ^[[B   forw-line
  352.       ^[[C   next-file
  353.       ^[[D   prev-file
  354.       ^[OA   back-line
  355.       ^[OB   forw-line
  356.       ^[OC   next-file
  357.       ^[OD   prev-file
  358.       ^[[6~  forw-scroll
  359.       ^[[5~  back-scroll
  360.       ^[[1~  goto-line
  361.       ^[[4~  goto-end
  362.   ______________________________________________________________________
  363.  
  364.  
  365.  
  366.   replacing '^[' with the ESC character    (ASCII 0x1B), then run ``lesskey
  367.   .lessrc'' in your home directory.  You can then use the cursor pad
  368.   keys for scrolling text under    less(1).
  369.  
  370.  
  371.  
  372.   3.  X    configuration
  373.  
  374.  
  375.   Under    the X windows system, every key    can have a different keysym
  376.   assigned to it, instead of just an ASCII value of key    sequence.  Thus,
  377.   X applications can distinguish easily    between, for example, <Tab>
  378.   (which generates the [Tab] keysym) and <Ctrl-i> (which generates the
  379.   [i] keysym with the "Ctrl" modifier).
  380.  
  381.   One consequence is that we want <BackSpace> to generate the
  382.   [BackSpace] keysym.  However,    on most    systems    <BackSpace> as well as
  383.   <Delete> generate the    [Delete] keysym    by default due to the way the
  384.   XFree86 server is written (it    reads the settings from    Linux's    key
  385.   map).    This makes the two keys    indistinguishable.  See    the entry for
  386.   xmodmap(1) for a fix.
  387.  
  388.   A list of keysyms can    be found in <X11/keysymdef.h> (usually
  389.   /usr/include/X11/keysymdef.h), without the XK_ prefix, and also
  390.   /usr/lib/X11/XKeysymDB.  Note    X keysyms are case sensitive.
  391.  
  392.   There    are several modifiers (like ``Shift'' and ``Ctrl'') that can be
  393.   generated under X: Shift, Ctrl, Meta,    Alt, Super and Hyper.  Note that
  394.   although ``Alt'' is present, most of the time    the <Alt> key on the PC
  395.   keyboard is bound to the ``Meta'' modifier; it works with more
  396.   applications this way.
  397.   3.1.    X server
  398.  
  399.  
  400.  
  401.      xev
  402.     xev(1) allows you to see events    generated on a window, including
  403.     <KeyPress> and <KeyRelease> events where you can find the
  404.     keycode    for a particular key.  Once the    keycode    is found for a
  405.     key it can be mapped into a X11    keysym with xmodmap.
  406.  
  407.  
  408.      xmodmap
  409.     One of the function of xmodmap(1) is to    map X keycodes to
  410.     keysyms.
  411.  
  412.     ``xmodmap <file>'' reads the keycode ->    keysym table from the
  413.     file ``file'' (usually named .xmodmaprc    or .Xmodmap in the
  414.     user's home directory).     This is usually done in
  415.     $(HOME)/.xinitrc when X    starts up.  (Warning: X    exits when the
  416.     last line of .xinitrc finishes,    so if you don't    have a .xinitrc
  417.     before,    you must put something that will continue to run (like
  418.     the window manager or an xterm)    at the end of .xinitrc,    and
  419.     don't put it in    the background!)
  420.  
  421.     Alternatively, you might generate a map    file from your
  422.     loadkey(1) *.map file using ``/usr/lib/kbd/keytables/mk_modmap
  423.     foo.map    > foo.xmap''; you need to remove all the ``compose''
  424.     command    in the output.    Note that <BackSpace> would be mapped to
  425.     the [Delete] keysym if you start with the keymap provided, so be
  426.     sure to    edit foo.xmap for keycode 22 (BackSpace) and 107
  427.     (Delete), or see below.
  428.  
  429.     ``xmodmap -e "command"'' can be    used to    execute    a single xmodmap
  430.     command, for simple changes.  For example, the following lines
  431.     in $HOME/.xinitrc will correct the <BackSpace> and <Delete> key
  432.     mappings:
  433.  
  434.  
  435.     ________________________________________________________________
  436.         # map the <BackSpace> key to the [BackSpace] keysym.
  437.         xmodmap -e "keycode    22 = BackSpace"
  438.         # map the <Delete> key to the [Delete] keysym.
  439.         xmodmap -e "keycode    107 = Delete"
  440.     ________________________________________________________________
  441.  
  442.  
  443.  
  444.  
  445.  
  446.   3.2.    X applications
  447.  
  448.  
  449.   X applications are usually configurated via resources.  User
  450.   customizations are usually put in $HOME/.Xdefaults and loaded    via
  451.   "xrdb    -load $HOME/.Xdefaults"    in .xinitrc (again, see    the warning in
  452.   the xmodmap entry).
  453.  
  454.  
  455.   3.2.1.  Terminal Emulators
  456.  
  457.  
  458.  
  459.   3.2.1.1.  xterm family
  460.  
  461.  
  462.  
  463.   Key mappings can be changed like this    in $(HOME)/.Xresources:
  464.  
  465.   ______________________________________________________________________
  466.       *VT100.Translations: #override <Key>BackSpace: string(0x7F)\n\
  467.           <Key>Delete:      string(0x1b) string("[3~")\n\
  468.           <Key>Home:      string(0x1b) string("[1~")\n\
  469.           <Key>End:          string(0x1b) string("[4~")\n\
  470.           Ctrl<Key>Prior:      string(0x1b) string("[40~")\n\
  471.           Ctrl<Key>Next:      string(0x1b) string("[41~")
  472.   ______________________________________________________________________
  473.  
  474.  
  475.  
  476.   This applies to most xterm-based emulators (xterm, color_xterm, kterm
  477.   etc. but not rxvt).
  478.  
  479.  
  480.   3.2.1.2.  rxvt
  481.  
  482.  
  483.   There    is apparaently no way to remap keys in rxvt except by patching
  484.   the source.  Any proof to the    contrary would be welcome.
  485.  
  486.  
  487.   3.2.2.  Editors
  488.  
  489.  
  490.  
  491.   3.2.2.1.  Emacs 19
  492.  
  493.  
  494.   global-set-key can be    used to    map keys to Emacs commands.  See the
  495.   Emacs    19 entry in the    non-X section.    To find    out what emacs symbol a
  496.   key sends, press the key in question (in a scratch buffer so it won't
  497.   clobber up your files) and then use the Emacs    command    view-lossage
  498.   (usually <Ctrl-h> <l>) to see    what the key generated.
  499.  
  500.   Usually the symbol is    the X11    keysym in lowercase; for example,
  501.   <Insert> (which generates the    [Insert] keysym) would be [insert] in
  502.   Emacs.
  503.  
  504.   <Shift-fkey>,    <Ctrl-fkey>, <Meta-fkey> generates [S-fkey], [C-fkey],
  505.   and [M-fkey] respectively.  Combinations generate something like [C-M-
  506.   fkey].  Other    modifiers are s- for "Super", H- for "Hyper" and A- for
  507.   "Alt".
  508.  
  509.  
  510.   3.2.3.  Other    Programs
  511.  
  512.  
  513.  
  514.   3.2.3.1.  Motif applications
  515.  
  516.  
  517.   Some people have problem with    using the <Delete> key in Motif
  518.   applications (most notably, Mosaic & Netscape). Ted Stern
  519.   <stern@amath.washington.edu> sent me this, which should fix the
  520.   problem when put in $HOME/.Xdefaults:
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.   ______________________________________________________________________
  530.       !
  531.       !     To make Backspace delete-previous-character and Delete
  532.       !     delete-next-character in Motif    applications generally ...
  533.       !
  534.       *XmText.translations:     #override \n\
  535.           ~a <Key>osfBackSpace: delete-previous-character()    \n\
  536.           ~a <Key>osfDelete: delete-next-character() \n\
  537.           a    <Key>osfDelete:    delete-previous-word() \n\
  538.           c    <Key>d:    delete-next-character()    \n\
  539.           a    <Key>d:    delete-next-word() \n\
  540.           c    <Key>a:    beginning-of-line() \n\
  541.           c    <Key>e:    end-of-line() \n\
  542.           c    <Key>k:    delete-to-end-of-line()    \n\
  543.           a    <Key>Left: backward-word() \n\
  544.           a    <Key>Right: forward-word()
  545.  
  546.       *XmTextField.translations:      #override    \n\
  547.           ~a <Key>osfBackSpace: delete-previous-character()    \n\
  548.           ~a <Key>osfDelete: delete-next-character() \n\
  549.           a    <Key>osfDelete:    delete-previous-word() \n\
  550.           c    <Key>d:    delete-next-character()    \n\
  551.           a    <Key>d:    delete-next-word() \n\
  552.           c    <Key>a:    beginning-of-line() \n\
  553.           c    <Key>e:    end-of-line() \n\
  554.           c    <Key>k:    delete-to-end-of-line()    \n\
  555.           a    <Key>Left: backward-word() \n\
  556.           a    <Key>Right: forward-word()
  557.   ______________________________________________________________________
  558.  
  559.  
  560.  
  561.   Remember that    this still requires the    <BackSpace> and    <Delete> key to
  562.   generate the correct keysyms ([BackSpace] and    [Delete] respectively).
  563.   See the xmodmap(1) entry.
  564.  
  565.  
  566.   4.  For more information
  567.  
  568.  
  569.   ``kbd.FAQ'' in the kbd package
  570.   (ftp://sunsite.unc.edu/pub/Linux/system/Keyboards/kbd-0.89.tar.gz)
  571.   contains more    examples on remapping the keyboard.
  572.  
  573.   Man pages for    the programs mentioned above are good sources of
  574.   information, especially xterm(1x), xmodmap and stty(1).
  575.  
  576.   Info pages for Emacs tells you how to    remap keys under it; use ``emacs
  577.   -f info'' to read them.
  578.  
  579.   ``Keystroke-HOWTO
  580.   (ftp://sunsite.unc.edu/pub/Linux/doc/HOWTO/mini/Keystroke)'' describes
  581.   a way    to assign special action to some of the    keys on    the keyboard.
  582.